﻿/*!	@file	r_gui_widget_window.cpp
	@brief	Window_GUI
Copyright (c) 2010 Yuya Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "r_gui_widget_window.h"
#include "r_gui_server.h"
#include "r_gui_draw.h"
#include "r_gui_event.h"

#include "r_gui_widget_poplist.h"

// >>>>>>>>>> RTTI >>>>>>>>>>
#include "../rtti/r_rtti_reflection_implement.h"
namespace r{
	namespace gui{
RRTTI_REFLECTION_IMPLEMENT ( r::gui::WidgetWindow, r::gui::Widget )
// <<<<<<<<<< RTTI <<<<<<<<<<

		const static	r_si32	s_rGUIWidgetWindowTextOffsetX	= 4;
		const static	r_si32	s_rGUIWidgetWindowTextOffsetY	= 2;
		const static	r_si32	s_rGUIWidgetWindowTextOffsetTop	= 16;

		const static	r_si32	s_rGUIWidgetWindowResizeTick	= 5;

		const static	r_si32	s_rGUIWidgetWindowCloseOffsetX	= 4;
		const static	r_si32	s_rGUIWidgetWindowCloseOffsetY	= 2;
		const static	r_si32	s_rGUIWidgetWindowCloseW		= 12;
		const static	r_si32	s_rGUIWidgetWindowCloseH		= 12;

		WidgetWindow :: WidgetWindow()
			:
			hoverClose ( false )
		{
			minWidth	= 100;
			minHeight	= 100;

			posX		= 0;
			posY		= 0;
			posW		= 100;
			posH		= 100;
			posOffR		= posOffL		= 4;
			posOffT		= 16;
			posOffB		= 4;
		}

		WidgetWindow :: ~WidgetWindow()
		{
		}

		typeConnection	WidgetWindow :: connectSlot ( const typeSignals<void() >::slot_type &slot )
		{
			return signalClose.connect ( slot );
		}

		void	WidgetWindow :: onUpdate ( Event& guiEvent )
		{
		}

		void	WidgetWindow :: onDraw ( draw::Draw& guidraw )
		{
			// 自分の領域にあわせて描画領域を作成
			guidraw.setZ ( guidraw.getZ() + 1 );
			guidraw.setRect ( wposX, wposY, posW, posH );
			guidraw.asignRect();

			guidraw.asignGUITexture();
			Server::instance		gui;
			const DrawInfo&		guiinfo = gui->getDrawInfo();
			guidraw.drawWindow ( wposX, wposY, posW, posH, draw::WIndows_Window, guiinfo.activeWindowColor );

			// closeボタン
			r_si32	closeX				= wposX + posW - s_rGUIWidgetWindowCloseOffsetX - s_rGUIWidgetWindowCloseW;
			r_si32	closeY				= wposY + s_rGUIWidgetWindowCloseOffsetY;
			if ( hoverClose == true ) {
				guidraw.drawIcon ( closeX, closeY, draw::Icons_Close, guiinfo.hoverWPartColor );
			} else {
				guidraw.drawIcon ( closeX, closeY, draw::Icons_Close, guiinfo.activeWPartColor );
			}

			if ( getNameLength() > 0 ) {
				// テキストの描画
				guidraw.drawText ( wposX + s_rGUIWidgetWindowTextOffsetX, wposY + s_rGUIWidgetWindowTextOffsetY, getName(), guiinfo.activeCharColor );
			}
			// 子に向けて描画領域の設定
			guidraw.setZ ( guidraw.getZ() + 1 );
			guidraw.setRect ( wposX + posOffL, wposY + posOffT, posW - ( posOffL + posOffR ), posH - ( posOffT + posOffB ) );
		}

		void	WidgetWindow :: onClickInRectLMB ( Event& guiEvent )
		{
			guiEvent.setLMBDrag ( this );
			r_si32	x;
			r_si32	y;
			guiEvent.getCurPos ( x, y );
			r_si32	tx	=	x - wposX;
			r_si32	ty	=	y - wposY;
			// closeボタン
			if (
				( ( tx >= ( posW - s_rGUIWidgetWindowCloseOffsetX - s_rGUIWidgetWindowCloseW ) ) )
				&& ( ( tx <= ( posW - s_rGUIWidgetWindowCloseOffsetX ) ) )
				&& ( ty >= s_rGUIWidgetWindowCloseOffsetY )
				&& ( ty <= s_rGUIWidgetWindowCloseOffsetY + s_rGUIWidgetWindowCloseH )
			) {
				hoverClose		= true;
			} else if ( ( tx >= ( posW - s_rGUIWidgetWindowResizeTick ) ) && ( ty >= ( posH - s_rGUIWidgetWindowResizeTick ) ) ) {
				drugMode		= eDrugMode_XYSize;
				drugOffsetX		= posW;
				drugOffsetY		= posH;
			} else if ( tx >= ( posW - s_rGUIWidgetWindowResizeTick ) ) {
				drugMode = eDrugMode_XSize;
				drugOffsetX		= posW;
			} else if ( ty >= ( posH - s_rGUIWidgetWindowResizeTick ) ) {
				drugMode = eDrugMode_YSize;
				drugOffsetY		= posH;
			} else {
				drugMode = eDrugMode_Move;
				drugOffsetX		= wposX - x;
				drugOffsetY		= wposY - y;
			}
		}

		void	WidgetWindow :: onDoubleClickInRectLMB ( Event& guiEvent )
		{
			// テストコード
			WidgetPopList*	poplist	= new WidgetPopList();
			MenuInfo	menuInfo;
			{
				MenuInfo	submenu;
				submenu.setName ( _RST ( "テスト" ) );
				{
					MenuInfo	_menu;
					_menu.setName ( _RST ( "テスト" ) );
					submenu.addSubMenu ( _menu );
				}
				menuInfo.addSubMenu ( submenu );
			}
			{
				MenuInfo	submenu;
				submenu.setType ( MenuType_Separator );
				menuInfo.addSubMenu ( submenu );
			}
			{
				MenuInfo	submenu;
				submenu.setName ( _RST ( "閉じる" ) );
				menuInfo.addSubMenu ( submenu );
			}
			poplist->setMenu ( menuInfo );
			r_si32	tx;
			r_si32	ty;
			guiEvent.getCurPos ( tx, ty );
			poplist->setPos ( tx, ty );
			Server::instance	gui;
			gui->addChild ( poplist );
		}

		void	WidgetWindow :: onReleaseInRectLMB ( Event& guiEvent )
		{
			r_si32	x;
			r_si32	y;
			guiEvent.getCurPos ( x, y );
			r_si32	tx	=	x - wposX;
			r_si32	ty	=	y - wposY;
			// closeボタン
			if (
				( ( tx >= ( posW - s_rGUIWidgetWindowCloseOffsetX - s_rGUIWidgetWindowCloseW ) ) )
				&& ( ( tx <= ( posW - s_rGUIWidgetWindowCloseOffsetX ) ) )
				&& ( ty >= s_rGUIWidgetWindowCloseOffsetY )
				&& ( ty <= s_rGUIWidgetWindowCloseOffsetY + s_rGUIWidgetWindowCloseH )
			) {
				doClose();
			}
			hoverClose	= false;
		}

		void	WidgetWindow :: onReleaseOutRectLMB ( Event& guiEvent )
		{
			hoverClose	= false;
		}

		void	WidgetWindow :: onDragLMB ( Event& guiEvent )
		{
			if ( hoverClose == true ) {
				return;
			}
			r_si32	b_x;
			r_si32	b_y;
			r_si32	n_x;
			r_si32	n_y;
			guiEvent.getDragPos ( b_x, b_y );
			guiEvent.getCurPos ( n_x, n_y );

			switch ( drugMode ) {
			case eDrugMode_Move:
				setPos ( n_x + drugOffsetX,	n_y + drugOffsetY );
				break;
			case eDrugMode_XSize:
				setSize ( n_x - b_x + drugOffsetX, posH );
				break;
			case eDrugMode_YSize:
				setSize ( posW, n_y - b_y + drugOffsetY );
				break;
			case eDrugMode_XYSize:
				setSize ( n_x - b_x + drugOffsetX, n_y - b_y + drugOffsetY );
				break;
			}
		}

		void	WidgetWindow :: onDragHoverInRectLMB ( Event& guiEvent )
		{
		}

		void	WidgetWindow :: onDragReleaseInRectLMB ( Event& guiEvent )
		{
		}

		void	WidgetWindow :: onHoverInRect ( Event& guiEvent )
		{
			r_si32	x;
			r_si32	y;
			guiEvent.getCurPos ( x, y );
			r_si32	tx	=	x - wposX;
			r_si32	ty	=	y - wposY;
			// closeボタン
			if (
				( ( tx >= ( posW - s_rGUIWidgetWindowCloseOffsetX - s_rGUIWidgetWindowCloseW ) ) )
				&& ( ( tx <= ( posW - s_rGUIWidgetWindowCloseOffsetX ) ) )
				&& ( ty >= s_rGUIWidgetWindowCloseOffsetY )
				&& ( ty <= s_rGUIWidgetWindowCloseOffsetY + s_rGUIWidgetWindowCloseH )
			) {
				hoverClose		= true;
			} else {
				hoverClose		= false;
			}
		}


		void	WidgetWindow :: doClose()
		{
			// クローズイベントをシグナルします
			signalClose();
			// 削除リストに登録します。
			Server::instance		guiserver;
			guiserver->remove ( this );
		}
	}
}
